23. Quiz: While Loops
Quiz: Count By
Suppose you want to count from some number
start_num
by another number
count_by
until you hit a final number
end_num
. Use
break_num
as the variable that you'll change each time through the loop. For simplicity, assume that
end_num
is always larger than
start_num
and
count_by
is always positive.
Before the loop, what do you want to set
break_num
equal to? How do you want to change
break_num
each time through the loop? What condition will you use to see when it's time to stop looping?
After the loop is done, print out
break_num
, showing the value that indicated it was time to stop looping. It is the case that
break_num
should be a number that is the first number larger than
end_num
.
Start Quiz:
start_num = #provide some start number
end_num = #provide some end number that you stop when you hit
count_by = #provide some number to count by
# write a while loop that uses break_num as the ongoing number to
# check against end_num
print(break_num)
Quiz: Count By Check
Suppose you want to count from some number
start_num
by another number
count_by
until you hit a final number
end_num
, and calculate
break_num
the way you did in the last quiz.
Now in addition, address what would happen if someone gives a
start_num
that is greater than
end_num
. If this is the case, set
result
to
"Oops! Looks like your start value is greater than the end value. Please try again."
Otherwise, set
result
to the value of
break_num
.
Start Quiz:
start_num = #provide some start number
end_num = #provide some end number that you stop when you hit
count_by = #provide some number to count by
# write a condition to check that end_num is larger than start_num before looping
# write a while loop that uses break_num as the ongoing number to
# check against end_num
print(result)
Quiz: Nearest Square
Write a
while
loop that finds the largest square number less than an integer
limit
and stores it in a variable
nearest_square
. A square number is the product of an integer multiplied by itself, for example 36 is a square number because it equals 6*6.
For example, if
limit
is 40, your code should set the
nearest_square
to 36.
Start Quiz:
limit = 40
# write your while loop here
print(nearest_square)